package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.RechargeRollbackResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 14:12
 */
public class RechargeRollbackBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(RechargeRollbackBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        Map<String, Object> stdCcrRechargeRefundMap = new HashMap<>();
        Map<String, Object> rechargeRefundMap = new HashMap<>();
        String reqSerial = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Reverse_Serial_Nbr");
        rechargeRefundMap.put("ReqSerial", reqSerial);
        String resSerial = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Serial_Nbr");
        rechargeRefundMap.put("ResSerial", resSerial);
        String destinationAccount = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Destination_Id");
        rechargeRefundMap.put("DestinationAccount", destinationAccount);
        String destinationAttr = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/DestinationAttr");
        rechargeRefundMap.put("DestinationAttr", destinationAttr);
        String BankCode = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Bank_code");
        rechargeRefundMap.put("BankCode", BankCode);
        stdCcrRechargeRefundMap.put("rechargeRefund", rechargeRefundMap);
        msgBody.put("stdCcrRechargeRefund", stdCcrRechargeRefundMap);


        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

        LOG.info("request body is {}", msgBodyStr.toString());
        LOG.info("webserviceUrl is {}", url);
        String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
        LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        RechargeRollbackResponse rollbackResponse = new Gson().fromJson(respStr, RechargeRollbackResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", rollbackResponse.getErrorCode());
        respDataXml.put("Reverse_Serial_Nbr", rollbackResponse.getStdCcaRechargeRefund().getReqSerial());
        respDataXml.put("Reverse_Date", rollbackResponse.getStdCcaRechargeRefund().getReverseDate());
        respDataXml.put("Reverse_Amount", rollbackResponse.getStdCcaRechargeRefund().getReverseAmount());
        respDataXml.put("Latn_Id", rollbackResponse.getStdCcaRechargeRefund().getLanId() == null ? "" : rollbackResponse.getStdCcaRechargeRefund().getLanId());

        String respXmlStr = "";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }
}
